package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.EduTeacherQueryVo;
import com.atguigu.eduservice.service.EduTeacherService;


import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author thomasyang
 * @since 2023-12-06
 */



@RestController
@RequestMapping("/eduservice/teacher")
//@CrossOrigin  //解决跨域
public class EduTeacherController {

    @Autowired
    private EduTeacherService teacherService;

    //1 查询讲师表所有数据
    @ApiOperation(value = "查询所有讲师列表")
    @GetMapping("findall")
    public R findAllTeacher() {

        List<EduTeacher> list = teacherService.list(null);

//        Map<String, Object> map = new HashMap<>();
//        map.put("items", list);

        return R.ok().data(list);
    }

    //2 逻辑删除讲师的方法
    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("/remove/{id}")
    public R removeById(@PathVariable String id) {

        boolean flag = teacherService.removeById(id);

        return  R.flagCheck(flag);
    }

    //3 分页查询讲师的方法
    //current 当前页
    //limit 每页记录数
    @ApiOperation(value = "分页查询讲师")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current, @PathVariable long limit) {

        Page<EduTeacher> pageTeacher = new Page<>(current, limit);

//        try {
//            int m = 10/0;
//        } catch (Exception e) {
//            throw new GuliException(201, "自定义异常");
//        }


        //调用方法实现分页
        //调用方法时候，底层封装，把分页所有数据封装到pageTeacher对象里面
        Page<EduTeacher> pageModel = teacherService.page(pageTeacher, null);

        return R.ok().data(pageModel);

    }

    //4 条件查询带分页的方法
    @ApiOperation(value = "带条件的讲师查询列表")
    @PostMapping(value = "pageTeacherCondition/{current}/{limit}")
    public R pageListTeacherCondition(@PathVariable long current, @PathVariable long limit,
                                      @RequestBody EduTeacherQueryVo eduTeacherQueryVo) {

        Page<EduTeacher> teacherPage = new Page<>(current, limit);
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();

        String teacherName = eduTeacherQueryVo.getName();
        Integer level = eduTeacherQueryVo.getLevel();
        String begin = eduTeacherQueryVo.getBegin();
        String end = eduTeacherQueryVo.getEnd();
        System.out.println("teacherName "+ teacherName );
        if (!StringUtils.isEmpty(teacherName)) {
            wrapper.like("name", teacherName);

        }
        if (!StringUtils.isEmpty(level)) {
            wrapper.like("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.like("begin", begin);
        }

        if (!StringUtils.isEmpty(end)) {
            wrapper.like("end", end);
        }

        Page<EduTeacher> page = teacherService.page(teacherPage, wrapper);

        return R.ok().data(page);


    }

    //添加讲师接口的方法
    @ApiOperation(value = "添加讲师")
    @PostMapping("save")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){

        System.out.println("eduteacher: " + eduTeacher.toString() );
        boolean flag = teacherService.save(eduTeacher);

        return R.flagCheck(flag);
    }
    @ApiOperation(value="根据id查询讲师")
    @GetMapping("/getTeacher/{id}")
    public R getTeacher(@PathVariable long id){

        EduTeacher teacher = teacherService.getById(id);

        return R.ok().data( teacher);
    }
    @ApiOperation(value = "根据id更新讲师")
    @PostMapping("update")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){

        boolean flag = teacherService.updateById(eduTeacher);

        return R.flagCheck(flag);
    }
    @ApiOperation(value = "根据id删除讲师")
    @DeleteMapping("delete/{id}")
    public R deleteTeacher(@PathVariable long id){

        boolean flag = teacherService.removeById(id);

        return R.flagCheck(flag);
    }


}

